package com.beagle.njddapp.ui.activity.oil;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.UploadBean;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.event.AddOilCarEvent;
import com.beagle.njddapp.ui.presenter.OilAddPresenter;
import com.beagle.njddapp.ui.view.OilAddView;
import com.beagle.njddapp.utils.GlideEngine;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.utils.Utils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-18 21:41
 * 加油车绑定
 */
public class OilAddActivity extends BaseMvpActivity<OilAddView, OilAddPresenter> implements OilAddView, View.OnClickListener {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.ll_oil_iv)
    LinearLayout lliv1;
    @BindView(R.id.ed_number)
    EditText edNUmber;
    @BindView(R.id.ed_changjia)
    EditText edChangJia;
    @BindView(R.id.ed_mingpai)
    EditText edMingPai;
    @BindView(R.id.ed_count)
    EditText edCount;
    @BindView(R.id.tv_time)
    TextView tvTIme;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private String path="";

    private static  final int CHOOSE_IMG=1004;
    private List<View> imgViews;
    private OilBean oilBean;
    @Override
    public int getLayoutId() {
        return R.layout.activity_addoil;
    }

    @Override
    public void init() {
        StatusBarUtil.setPaddingSmart(this,container);
        container.setBackgroundColor(Color.WHITE);
        StatusBarUtil.darkMode(this);
        imgViews=new ArrayList<>();
        oilBean= (OilBean) getIntent().getSerializableExtra("data");
        if (oilBean!=null){
            tvTitle.setText("加油车修改");
            tvRight.setText("完成");
            edNUmber.setClickable(false);
            tvNext.setVisibility(View.GONE);
            edNUmber.setText(oilBean.getCarNum());
            edChangJia.setText(oilBean.getProductionFactory());
            edCount.setText(oilBean.getOilMeter()+"");
            edMingPai.setText(oilBean.getEngineNum());
            tvTIme.setText(oilBean.getProductionDate());
            if (TextUtils.isEmpty(oilBean.getPictureUrl())){
                addFirstIv();
            }else {
                path=oilBean.getPictureUrl();
                addIv(path);
            }

        }else {
            tvTitle.setText("绑定加油车");
            addFirstIv();

        }
    }
    private void addFirstIv() {

        View view= LayoutInflater.from(this).inflate(R.layout.item_add_img,null);
        ImageView ivAdd=view.findViewById(R.id.iv_add);
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Matisse.from(OilAddActivity.this)
                        .choose(MimeType.ofImage())
                        .capture(true)
                        .countable(true)
                        .maxSelectable(1)
                        .thumbnailScale(0.85f)
                        .captureStrategy(new CaptureStrategy(true, "com.beagle.njddapp"))
                        .imageEngine(GlideEngine.getInstance())
                        .showPreview(false) // Default is `true`
                        .forResult(CHOOSE_IMG);
            }
        });
        imgViews.add(view);
        lliv1.addView(view);
    }

    @OnClick({R.id.tv_next,R.id.common_tv_right,R.id.common_back_iv,R.id.tv_time})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.tv_time:
                chooseTime();
                break;
            case R.id.tv_next:
                if (TextUtils.isEmpty(edNUmber.getText().toString())||TextUtils.isEmpty(edMingPai.getText().toString())||
                        TextUtils.isEmpty(edChangJia.getText().toString())||TextUtils.isEmpty(tvTIme.getText().toString())||
                        TextUtils.isEmpty(edCount.getText().toString())){
                    ToastUtils.showShort("请输入完整信息");
                    return;
                }
                String str=edNUmber.getText().toString();
//                if (Utils.getHanZi(str)!=1){
//                    ToastUtils.showShort("请输入正确的车牌信息");
//                }
                OilBean oilOrderBean=new OilBean();
                oilOrderBean.setCarNum(edNUmber.getText().toString());
                oilOrderBean.setEngineNum(edMingPai.getText().toString());
                oilOrderBean.setProductionDate(tvTIme.getText().toString()+ " 00:00:00");
                oilOrderBean.setOilMeter(Integer.parseInt(edCount.getText().toString()));
                oilOrderBean.setProductionFactory(edChangJia.getText().toString());
                oilOrderBean.setPictureUrl(path);
                presenter.addOil(oilOrderBean);
                break;
            case R.id.common_tv_right:
                if (TextUtils.isEmpty(edNUmber.getText().toString())||TextUtils.isEmpty(edMingPai.getText().toString())||
                        TextUtils.isEmpty(edChangJia.getText().toString())||TextUtils.isEmpty(tvTIme.getText().toString())||
                        TextUtils.isEmpty(edCount.getText().toString())){
                    ToastUtils.showShort("请输入完整信息");
                    return;
                }
                OilBean oilOrderBean2=new OilBean();
                oilOrderBean2.setCarNum(edNUmber.getText().toString());
                oilOrderBean2.setEngineNum(edMingPai.getText().toString());
                oilOrderBean2.setProductionDate(tvTIme.getText().toString()+ " 00:00:00");
                oilOrderBean2.setOilMeter(Integer.parseInt(edCount.getText().toString()));
                oilOrderBean2.setProductionFactory(edChangJia.getText().toString());
                oilOrderBean2.setPictureUrl(path);
                presenter.updateOilCar(oilOrderBean2);
                break;
        }
    }

    @Override
    public OilAddView getMvpView() {
        return this;
    }

    @Override
    public OilAddPresenter createPresenter() {
        return new OilAddPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data==null){
            return;
        }
        if (requestCode==CHOOSE_IMG&&data!=null){
            List<Uri> mSelected = Matisse.obtainResult(data);
            if (mSelected!=null&&mSelected.size()>0){
                addIv(mSelected.get(0));
                presenter.upload(Utils.getPathFromUri(this,mSelected.get(0)));

            }
        }
    }

    private void addIv(Uri uri) {
            imgViews.clear();
            lliv1.removeAllViews();
            imgViews.add(getImgView(uri));
            for (View view:imgViews){
                lliv1.addView(view);
            }

    }
    private void addIv(String path) {
        imgViews.clear();
        lliv1.removeAllViews();
        imgViews.add(getImgView(path));
        for (View view:imgViews){
            lliv1.addView(view);
        }

    }
    public View getImgView(String path){
        View view=LayoutInflater.from(this).inflate(R.layout.item_add_img,null);
        view.setTag(path);
        ImageView ivimg=view.findViewById(R.id.iv_img);
        Glide.with(this).load(path).into(ivimg);
        ImageView ivcancle=view.findViewById(R.id.iv_cancle);
        ivcancle.setVisibility(View.VISIBLE);
        ImageView ivadd=view.findViewById(R.id.iv_add);
        ivadd.setVisibility(View.GONE);
        ivcancle.setOnClickListener(this);
        ivcancle.setTag(path);
        return view;
    }
    public View getImgView(Uri uri){
        View view=LayoutInflater.from(this).inflate(R.layout.item_add_img,null);
        view.setTag(uri);
        ImageView ivimg=view.findViewById(R.id.iv_img);
        Glide.with(this).load(uri).into(ivimg);
        ImageView ivcancle=view.findViewById(R.id.iv_cancle);
        ivcancle.setVisibility(View.VISIBLE);
        ImageView ivadd=view.findViewById(R.id.iv_add);
        ivadd.setVisibility(View.GONE);
        ivcancle.setOnClickListener(this);
        ivcancle.setTag(uri);
        return view;
    }

    private void chooseTime() {
        Calendar calendar=Calendar.getInstance();
        DatePickerDialog datePickerDialog=new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String monthStr;
                if (month<9){
                    monthStr="0"+(month+1);
                }else {
                    monthStr=(month+1)+"";
                }

                tvTIme.setText(year+"-" + monthStr + "-" + dayOfMonth);
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
        datePickerDialog.show();
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.iv_cancle){
            lliv1.removeAllViews();
            addFirstIv();
            imgViews.clear();
            path="";
        }
    }

    @Override
    public void showSuccess() {
        ToastUtils.showShort("添加成功");
        EventBus.getDefault().post(new AddOilCarEvent());
        setResult(303);
        finish();
    }

    @Override
    public void UploadSuccess(UploadBean data) {
        path=data.getVisitURL();
    }
}
